﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jamaat_Financial_System.Model.BO;
using System.Collections.ObjectModel;
using System.Data.OleDb;
using Jamaat_Financial_System.Model.Util;

namespace Jamaat_Financial_System.Model.DAO
{
    class Tajneed_DAO : Base_DAO
    {
        public ObservableCollection<Model.BO.Base_BO> _TanjeedList = new ObservableCollection<Model.BO.Base_BO>();

        public override ObservableCollection<Model.BO.Base_BO> UpdateList()
        {
            command = new OleDbCommand("SELECT * FROM '" + Constants.TableNames.TAJNEED + "'", connection);
            OleDbDataReader reader = command.ExecuteReader();
            Tajneed_BO tajneedBO = null;
            while (reader.Read())
            {
                // Initializing new tajneed
                tajneedBO = new Tajneed_BO();

                //setters
                tajneedBO.Name = reader.GetString(Constants.TajneedTble.NAME);
                tajneedBO.FatherName = reader.GetString(Constants.TajneedTble.FATHERNAME);
                tajneedBO.Address = reader.GetString(Constants.TajneedTble.ADDRESS);
                tajneedBO.City = reader.GetString(Constants.TajneedTble.CITY);
                tajneedBO.Tell = reader.GetString(Constants.TajneedTble.TELLNO);
                tajneedBO.Mobile = reader.GetString(Constants.TajneedTble.MOBILE);
                tajneedBO.HalqaId = reader.GetString(Constants.TajneedTble.HALQAID);
                tajneedBO.Tanzeem = reader.GetString(Constants.TajneedTble.TELLNO);
                tajneedBO.TajId = reader.GetString(Constants.TajneedTble.TAJID);
                tajneedBO.Wasiat = reader.GetString(Constants.TajneedTble.WASIAT).Equals("Yes") ? true : false;
                tajneedBO.WasiatNo = reader.GetString(Constants.TajneedTble.WASIATNO);
                tajneedBO.WasiatHisa = reader.GetInt32(Constants.TajneedTble.WASIATHISA);

                _TanjeedList.Add(tajneedBO);
            }

            return _TanjeedList; 
        }

        public Model.BO.Base_BO GetSingleTajneedRecord(Tajneed_BO _tajneedBO)
        {
            command = new OleDbCommand("SELECT * FROM '" + Constants.TableNames.TAJNEED + "' where tajId = '" + _tajneedBO.TajId + "'", connection);
            OleDbDataReader reader = command.ExecuteReader();
            // Initializing  tajneed
            Tajneed_BO tajneedBO = new Tajneed_BO();
            if (reader.Read())
            {                
                //setters
                tajneedBO.Name = reader.GetString(Constants.TajneedTble.NAME);
                tajneedBO.FatherName = reader.GetString(Constants.TajneedTble.FATHERNAME);
                tajneedBO.Address = reader.GetString(Constants.TajneedTble.ADDRESS);
                tajneedBO.City = reader.GetString(Constants.TajneedTble.CITY);
                tajneedBO.Tanzeem = reader.GetString(Constants.TajneedTble.TANZEEM);
                tajneedBO.Mobile = reader.GetString(Constants.TajneedTble.MOBILE);
                tajneedBO.Wasiat = reader.GetString(Constants.TajneedTble.WASIAT).Equals("Yes") ? true : false;
                tajneedBO.Tell = reader.GetString(Constants.TajneedTble.TELLNO);
                tajneedBO.WasiatNo = reader.GetString(Constants.TajneedTble.WASIATNO);
                tajneedBO.HalqaId = reader.GetString(Constants.TajneedTble.HALQAID);
                tajneedBO.TajId = reader.GetString(Constants.TajneedTble.TAJID);
                tajneedBO.WasiatHisa = reader.GetInt32(Constants.TajneedTble.WASIATHISA);
                tajneedBO.Transfered = reader.GetString(Constants.TajneedTble.TRANSFERED).Equals("Yes") ? true : false;
            }

            return tajneedBO;
        }

        public override int Insert(Model.BO.Base_BO _record)
        {
            try
            {
                Model.BO.Tajneed_BO record = (Model.BO.Tajneed_BO)_record;

                command = new OleDbCommand("INSERT INTO " + Constants.TableNames.TAJNEED + "(TajID, Name, FathersName, Address, City, Tel, Mobil, WasiyyatHissa, WasiyyatNo, Wasiyyat, Tanzeem, HalqaID) VALUES('" + record.TajId + "','" + record.Name + "','" + record.FatherName + "','" + record.Address + "','" + record.City + "','" + record.Tell + "','" + record.Mobile + "'," + record.WasiatHisa + ",'" + record.WasiatNo + "'," + (record.Wasiat == true) + ",'" + record.Tanzeem + "','" + record.HalqaId + "')", connection);
                if (command.ExecuteNonQuery() != 1)
                {
                    return ErrorCodes_BO.FAILED_TO_INSERT_RECORD;
                }

                return ErrorCodes_BO.SUCCESS;
            }
            catch (OleDbException ex)
            {
                return ErrorCodes_BO.DUPLICATE_KEY;
            }
            catch (Exception ex)
            {
                return ErrorCodes_BO.FAILED_TO_INSERT_RECORD;
            }
        }
        public override int Delete(Model.BO.Base_BO _record)
        {
            return 0;
        }
    }
}
